#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

struct Task {
    int id;
    std::string description;
    bool completed;
};

class TodoList {
private:
    std::vector<Task> tasks;
    const std::string filename = "todo.dat";
    int nextId = 1;

public:
    TodoList() {
        loadTasks();
    }

    ~TodoList() {
        saveTasks();
    }

    void addTask(const std::string& description) {
        Task newTask;
        newTask.id = nextId++;
        newTask.description = description;
        newTask.completed = false;
        tasks.push_back(newTask);
        std::cout << "任务已添加！ID: " << newTask.id << std::endl;
    }

    void deleteTask(int id) {
        for (auto it = tasks.begin(); it != tasks.end(); ++it) {
            if (it->id == id) {
                tasks.erase(it);
                std::cout << "任务已删除！ID: " << id << std::endl;
                return;
            }
        }
        std::cout << "未找到ID为 " << id << " 的任务！" << std::endl;
    }

    void markComplete(int id) {
        for (auto& task : tasks) {
            if (task.id == id) {
                task.completed = true;
                std::cout << "任务已标记为完成！ID: " << id << std::endl;
                return;
            }
        }
        std::cout << "未找到ID为 " << id << " 的任务！" << std::endl;
    }

    void viewAllTasks() {
        if (tasks.empty()) {
            std::cout << "当前没有任务！" << std::endl;
            return;
        }

        std::cout << "\n=== 待办清单 ===" << std::endl;
        for (const auto& task : tasks) {
            std::cout << "ID: " << task.id 
                      << " | 状态: " << (task.completed ? "[√]" : "[ ]") 
                      << " | 描述: " << task.description << std::endl;
        }
        std::cout << "================" << std::endl;
    }

    void loadTasks() {
        std::ifstream file(filename);
        if (!file.is_open()) {
            return;
        }

        std::string line;
        while (std::getline(file, line)) {
            std::istringstream iss(line);
            Task task;
            char delimiter;
            iss >> task.id >> delimiter;
            std::getline(iss, task.description, '|');
            iss >> task.completed;
            tasks.push_back(task);
            nextId = std::max(nextId, task.id + 1);
        }
        file.close();
    }

    void saveTasks() {
        std::ofstream file(filename);
        if (!file.is_open()) {
            std::cerr << "无法保存任务到文件！" << std::endl;
            return;
        }

        for (const auto& task : tasks) {
            file << task.id << "|" << task.description << "|" << task.completed << "\n";
        }
        file.close();
    }
};

void displayMenu() {
    std::cout << "\n=== 待办清单工具 ===" << std::endl;
    std::cout << "1. 添加任务" << std::endl;
    std::cout << "2. 删除任务" << std::endl;
    std::cout << "3. 标记完成" << std::endl;
    std::cout << "4. 查看全部任务" << std::endl;
    std::cout << "5. 退出" << std::endl;
    std::cout << "请选择操作: ";
}

int main() {
    TodoList todo;
    int choice;

    while (true) {
        displayMenu();
        std::cin >> choice;
        std::cin.ignore(); // 清除换行符

        switch (choice) {
            case 1: {
                std::string description;
                std::cout << "请输入任务描述: ";
                std::getline(std::cin, description);
                todo.addTask(description);
                break;
            }
            case 2: {
                int id;
                std::cout << "请输入要删除的任务ID: ";
                std::cin >> id;
                todo.deleteTask(id);
                break;
            }
            case 3: {
                int id;
                std::cout << "请输入要标记完成的任务ID: ";
                std::cin >> id;
                todo.markComplete(id);
                break;
            }
            case 4:
                todo.viewAllTasks();
                break;
            case 5:
                std::cout << "感谢使用！再见！" << std::endl;
                return 0;
            default:
                std::cout << "无效的选择，请重试！" << std::endl;
        }
    }

    return 0;
}
